$(function () {
    let layer = layui.layer
    let form = layui.form
    initArtCateList();
    function initArtCateList() {
        axios({
            method: 'GET',
            url: '/my/article/cates'
        }).then(res => {
            if (res.data.status != 0) {
                return layer.msg(res.data.message)
            }
            let arr = ['<option value="">请选择文章类别</option>']
            res.data.data.forEach(ele => {
                arr.push(`<option value="${ele.Id}">${ele.name}</option>`)
            })
            let str = arr.join('')
            $('[name=cate_id]').empty().html(str)
            form.render()
        })

    }
    initEditor()
    var $image = $('#image')
    var options = {
        aspectRatio: 400 / 280,
        preview: '.img-preview'
    }
    $image.cropper(options)

    let state
    $('#btnSavel1').on('click', function () {
        state = '已发布'
    })
    $('#btnSavel2').on('click', function () {
        state = '草稿'
    })
    $('#chooseImage').on('click', function () {
        $('#file').click()
    })
    $('#file').on('change', function (e) {
        var file = e.target.files[0]
        if (file == undefined) {
            return layer.msg('此文章封面为空')
        }
        var newImgURL = URL.createObjectURL(file)
        $image
            .cropper('destroy')      // 销毁旧的裁剪区域
            .attr('src', newImgURL)  // 重新设置图片路径
            .cropper(options)        // 重新初始化裁剪区域
    })
    $('#formPub').on('submit', function (e) {
        e.preventDefault()
        let fd = new FormData(this)
        fd.append('state', state)
        $image
            .cropper('getCroppedCanvas', { // 创建一个 Canvas 画布
                width: 400,
                height: 280
            })
            .toBlob(function (blob) {
                fd.append('cover_img', blob)
                console.log(...fd);

                axios({
                    method: 'POST',
                    url: '/my/article/edit',
                    data: fd
                }).then(res => {
                    if (res.data.status != 0) {
                        return layer.msg(res.data.message)
                    }
                    layer.msg('修改成功')

                    setTimeout(function () {
                        window.parent.document.querySelector('#art_list').click()
                    }, 1500)
                })
                // let baseURL = 'http://api-breakingnews-web.itheima.net'
                // $.ajax({
                //     method: 'POST',
                //     url: baseURL + '/my/article/add',
                //     contentType: false,
                //     processData: false,
                //     data: fd,
                //     headers: {
                //         Authorization: localStorage.getItem('token')
                //     },
                //     success: function (res) {
                //         console.log(res);
                //         layer.msg(res.message)
                //     }
                // })
            })
    })
}
)